package com.xbdun.web.validatebmp.facade.qr;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.facade.validatebmp.exceptions.ValidateBizException;
import com.xbdun.facade.validatebmp.request.GenroScanQrRequest;
import com.xbdun.facade.validatebmp.response.WxUnionCallPcResponse;
import com.xbdun.facade.validatebmp.result.QrCodeResult;
import com.xbdun.facade.validatebmp.result.WxUnionCallPcResult;
import com.xbdun.facade.validatebmp.service.qr.QrCodeFacade;
import com.xbdun.web.validatebmp.service.QrCodeService;

import lombok.extern.slf4j.Slf4j;
/**
 * 生成二维码
 * @author Zhangguotao(King)
 * @2014年10月23日
 */
@Component
@Slf4j
public class QrCodeFacadeImpl implements QrCodeFacade {
	@Autowired
	QrCodeService qrCodeService;
	
	/**
	* 设置返回二维码字符串
	* @Title: encodeByte
	* @Description	设置返回二维码字符串
	* @param request
	* @return    设定文件
	* @return QrCodeResult    返回类型
	*/
	public QrCodeResult encodeByte(GenroScanQrRequest request){
		QrCodeResult result = new QrCodeResult();
		result.setBizException(BizException.ERROR);
		log.info("二维码生成返回字符，入参：{}",request);
		try{
			result.setData(qrCodeService.encodeByte(request));
		}catch(Exception e){
			result.setBizException(e);
			log.error("二维码生成返回字符，异常：{}",e);
		}
		//
		return result;
	}
	
	public WxUnionCallPcResult getQrInfo(String redisKey){
		WxUnionCallPcResult result = new WxUnionCallPcResult();
		result.setBizException(ValidateBizException.QR_NOT_EXIST);
		try{
			if(StringUtils.isBlank(redisKey)){
				throw BizException.PARAM_CHKE_FAIL.newInstance("二维码KEY不能为空");
			}
			//根据KEY获取扫码信息
			WxUnionCallPcResponse qrValue = qrCodeService.getQrValue(redisKey);
			if(null == qrValue){
				throw ValidateBizException.QR_NOT_EXIST.newInstance("二维码不存在，可能已经过期,请重新生成");
			}
			result.setBizException(BizException.SUCCESS);
			result.setData(qrValue);
		}catch(Exception e){
			result.setBizException(e);
		}
		return result;
	}
}
